/**
 * StarListGUI
 * 
 * @author
 * 
 */
package assign3;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;

public class StarListGUI<T> extends JFrame implements ActionListener {
	/**
	 * Data members
	 */
	private static final long serialVersionUID = 1L;
	private final int FRAME_X=100, FRAME_Y=100, FRAME_WIDTH=400,  FRAME_HEIGHT=300;
	private final int SCROLL_X=30, SCROLL_Y=30, SCROLL_WIDTH=300, SCROLL_HEIGHT=200;
	private final int SORT_X=30,   SORT_Y=235,  SORT_WIDTH=100,   SORT_HEIGHT=30;
	private String SORT_STRING = "Sort";
	private String SHUFFLE_STRING = "Shuffle";
	private JList listview;
	private JScrollPane scroll;
	
	private JButton sort;
	
	
	String[] stars = {
			"Polaris Australis", "Pollux", "Rana", "Sirius", "Yed Prior",
			"Electra", "Gacrux", "Gianfar", 
			"Maia", "Merga", "Nash", "Nodus Secundus", 
			"Peacock", "Pherkad Minor", "Pleione", 
			"Gienah Cygni", "Grafias", "Head of Hydrus",
			"Herschel's Garnet Star", "Hyadum II", "Kaus Borealis"
	};
	
	
	DefaultListModel listmodel;

	/**
	 * StarListGUI constructor
	 * 
	 * @param title
	 *
	 */
	public StarListGUI(String title) {
		super(title);
		setBounds(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setLayout( null );
		
		/*
		 * Data that will be used as the "List Model"
		 */
		
		listmodel = new DefaultListModel();
		for (String s : stars)
			listmodel.addElement( s );

		/*
		 * Now that the data is available, set up GUI elements to display it.
		 */
		listview = new JList( listmodel );
		listview.setVisible(true);
		scroll = new JScrollPane( listview );
		scroll.setBounds(SCROLL_X, SCROLL_Y, SCROLL_WIDTH, SCROLL_HEIGHT);
		add( scroll );
		
		sort = new JButton( SORT_STRING );
		sort.addActionListener( this );
		sort.setBounds(SORT_X, SORT_Y, SORT_WIDTH, SORT_HEIGHT);
		add(sort);

		
		setVisible( true );
	}

	/**
	 * actionPerformed - allos for buttons to have actions associated with them
	 * 
	 * @require ae != null
	 */
	public void actionPerformed(ActionEvent ae) {
		Object source = ae.getSource();
		if (source.equals(sort)){
			if (sort.getText().equals( SORT_STRING )){
				sort.setText(SHUFFLE_STRING);
				
				Vector<String> newstars = new Vector<String>();
				for(int i = 0; i < listmodel.size(); i++){
					newstars.add((String) listmodel.elementAt(i));
				}
				 /*
				  * Sort
				  */
				Collections.sort( newstars );
				
				listmodel.removeAllElements();
				
				for(String s : newstars)
					listmodel.addElement(s);
				
			} else {
				if (sort.getText().equals( SHUFFLE_STRING )){
					sort.setText( SORT_STRING );
					
					Vector<String> newstars = new Vector<String>();
					for(int i = 0; i < listmodel.size(); i++){
						newstars.add((String) listmodel.elementAt(i));
					}
				/*
				 * Shuffle
				 */
					Collections.shuffle( newstars );
					
					listmodel.removeAllElements();
					
					for(String s : newstars)
						listmodel.addElement(s);
				}
				
			}
		}	
	}

	/**
	 * main method
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new StarListGUI("My Star System");
	}
}
